package com.lovo.controller;

import com.lovo.entity.UserEntity;
import com.lovo.token.SysToken;
import com.lovo.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("boot")
@Api(tags = "上传接口")
@Slf4j
public class UploadController {

    //get post put delete
    @ApiOperation(value = "文件上传")
    @PostMapping("upload")
    public String upload(@RequestParam("file")MultipartFile file) throws IOException {
        //判断格式 --防止黑客攻击
        //重命名--->防止覆盖
        //判断文件是否存在
        String os = System.getProperty("os.name");
        File f=null;
        if(os != null && os.toLowerCase().startsWith("windows")){
            f = new File(FileUtil.W_DIR+"/"+file.getOriginalFilename());
        }else {
            f = new File(FileUtil.L_DIR+"/"+file.getOriginalFilename());
        }
        file.transferTo(f);
        return file.getOriginalFilename();
    }

    @ApiOperation(value = "AOP测试")
    @GetMapping("testInterceptor")
    public String testInterceptor(){

        return "hello AOP";
    }

    @ApiOperation(value = "用户信息查询")
    @GetMapping("userInfo/{userName}/{password}/info")
    public String userInfo(@PathVariable("userName") String userName,
                           @PathVariable("password")String password,
                           HttpServletRequest request){
        String path=  request.getServletPath();
        log.info(path);
        //验证
        String  token=request.getHeader("token");
        if(SysToken.verifierJwt(token).equals("502")){
            return "";
        }
        String user="";
        if(userName.equals("zy")){
            user="赵云";
        }else if(userName.equals("lc")){
            user="林冲";
        }else {
            user="马超";
        }
        return user;
    }

    @ApiOperation(value = "保存用户")
    @PostMapping("saveUser")
    public boolean saveUser(UserEntity user){
        log.info(user.getUserName());
        return true;
    }
}